<template>
    <div>
         <!--面包屑导航栏区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!--卡片区域-->
        <el-card>
            <!--添加角色按钮-->
            <el-row>
                <el-col><el-button type="primary" @click="showAddDialog">添加分类</el-button></el-col>
            </el-row>
            <!--表格区域-->
            <tree-table
                ref="treeTableRef"
                :data="categoriesList"
                :columns="columns"
                show-index
                index-text="#"
                border
                :selection-type="false"
                :expand-type="false"
                :show-row-hover="false">
                <!--是否有效列表-->
                <template slot="isOk" slot-scope="scope">
                    <i v-if="scope.row.cat_deleted === false" class="el-icon-success" style="color: lightgreen;"></i>
                    <i v-else class="el-icon-error" style="color: red;"></i>
                </template>
                <!--当前层级列表-->
                <template slot="level" slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
                    <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
                    <el-tag size="mini" type="warning" v-else>三级</el-tag>
                </template>
                <!--操作列表-->
                <template slot="operation" slot-scope="scope">
                    <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" icon="el-icon-delete" @click="showDeleteDialog(scope.row)">删除</el-button>
                </template>
            </tree-table>
            <!--分页区域-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
        </el-card>
        <!--添加分类对话框-->
        <el-dialog
            title="添加分类"
            :visible.sync="addDialogVisible"
            width="50%">
            <!--验证表单-->
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
            <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addForm.cat_name"></el-input>
            </el-form-item>
            <el-form-item label="父级分类">
            <el-cascader
                v-model="selectedOption"
                :options="ParentCategoryList"
                :props="cascaderprops"
                clearable
                change-on-select
                @change="parentCategoryChanged"></el-cascader>
            </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible= false">取 消</el-button>
            <el-button type="primary" @click="addCategory">确 定</el-button>
            </span>
        </el-dialog>
        <!--编辑分类对话框-->
        <el-dialog
            title="编辑分类"
            :visible.sync="editDialogVisible"
            width="50%">
            <!--验证表单-->
            <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="80px">
            <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="editForm.cat_name"></el-input>
            </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible= false">取 消</el-button>
            <el-button type="primary" @click="editCategory">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data () {
        return {
            queryInfo: {
                type: 3,
                pagenum: 1,
                pagesize: 5
            },
            categoriesList: [],
            total: 0,
            columns: [
          {
            label: '分类名称',
            prop: 'cat_name',
            width: '250px'
          },
          {
            label: '是否有效',
            minWidth: '80px',
            type: 'template',
            template: 'isOk'
          },
          {
            label: '当前层级',
            minWidth: '80px',
            type: 'template',
            template: 'level'
          },
          {
            label: '操作',
            minWidth: '80px',
            type: 'template',
            template: 'operation'
          }
        ],
        addForm: {
            cat_name: '',
            cat_pid: 0,
            cat_level: 0
        },
        selectedOption: [],
        addDialogVisible: false,
        addFormRules: {
            cat_name: [
                { required: true, message: '请输入分类名称', trigger: 'blur' },
                { min: 1, max: 10, message: '长度在1到10个字符', trigger: 'blur' }
            ]
        },
        ParentCategoryList: [],
        cascaderprops: {
            value: 'cat_id',
            label: 'cat_name',
            children: 'children',
            expandTrigger: 'hover'
        },
        editDialogVisible: false,
        editForm: {}
        }
    },
    created () {
        this.getCategoriesList()
    },
    methods: {
        // 获取商品分类列表
        async getCategoriesList () {
            const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
            if (res.meta.status !== 200) return this.$message.error('获取商品分类列表失败')
            this.categoriesList = res.data.result
            this.total = res.data.total
            this.options = res.data.result.cat_name
        },
        // 监听用户列表的显示条数
        handleSizeChange (newSize) {
            this.queryInfo.pagesize = newSize
            this.getCategoriesList()
        },
        // 监听用户列表的当前页数
        handleCurrentChange (newPage) {
            this.queryInfo.pagenum = newPage
            this.getCategoriesList()
        },
        // 打开添加分类对话框
        showAddDialog () {
            this.addDialogVisible = true
            this.getParentCategoryList()
        },
        // 获取父级分类列表
        async getParentCategoryList () {
            const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
            if (res.meta.status !== 200) return this.$message.error('获取父级分类列表失败')
            this.ParentCategoryList = res.data
        },
        // 改变父级分类选项
        parentCategoryChanged () {
            if (this.selectedOption.length > 0) {
                this.addForm.cat_pid = this.selectedOption[this.selectedOption.length - 1]
                this.addForm.cat_level = this.selectedOption.length
                return false
            } else {
                this.addForm.cat_pid = 0
                this.addForm.cat_level = 0
            }
        },
        // 提交添加的分类
        async addCategory () {
            const { data: res } = await this.$http.post('categories', this.addForm)
            if (res.meta.status !== 201) return this.$message.error('添加分类失败')
            this.$message.success('添加分类成功')
            this.getCategoriesList()
            this.addDialogVisible = false
        },
        // 打开编辑分类对话框
        async showEditDialog (row) {
            this.editDialogVisible = true
            const { data: res } = await this.$http.get(`categories/${row.cat_id}`)
            if (res.meta.status !== 200) return this.$message.error('获取分类名称失败')
            this.editForm = res.data
            console.log(this.editForm)
        },
        // 提交编辑分类对话框
        async editCategory () {
            const { data: res } = await this.$http.put('categories/' + this.editForm.cat_id, { cat_name: this.editForm.cat_name })
            if (res.meta.status !== 200) return this.$message.error('修改分类名称失败')
            this.$message.success('修改分类名称成功')
            this.getCategoriesList()
            this.editDialogVisible = false
        },
        // 删除分类
        async showDeleteDialog (row) {
            const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).catch(err => err)
            // 用户确认删除，返回'confirm'，用户取消删除，返回'cancel'
            if (result !== 'confirm') return this.$message.info('取消删除分类')
            const { data: res } = await this.$http.delete(`categories/${row.cat_id}`)
            if (res.meta.status !== 200) return this.$message.error('删除用户信息失败')
            this.$message.success('删除用户信息成功')
            this.getCategoriesList()
        }
    }
}
</script>
<style lang="less" scoped>
.el-row{
    margin-bottom: 15px;
}
.el-cascader{
    width: 100%;
}
</style>
